package petstore.web.servlet.login;

import petstore.domain.Log;
import petstore.persistence.LogDaoImpl;
import petstore.persistence.user.User;
import petstore.persistence.user.UserDAOImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;

//登录界面逻辑处理
public class LoginServlet extends HttpServlet {
    private int uid;
    private String username;//用户名
    private String password;//密码
    private String code;//验证码
    private String loginMsg;//反馈消息
    private String loginCode;//从session获取生成的验证码

    User user;//用户类

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username=req.getParameter("username");//从提交上来的form中获取用户名
        this.password=req.getParameter("password");//从提交上来的form中获取密码
        this.code=req.getParameter("code");//从提交上来的form中获取验证码
        //从session中获取生成的验证码
        HttpSession session= req.getSession();
        this.loginCode=(String) session.getAttribute("validateCode");


        //判断用户名、密码是否正确合法的逻辑
        if(!isValid()){
            req.setAttribute("loginMsg",loginMsg);
            req.getRequestDispatcher("login").forward(req,resp);
        }else{
            //如果合法，查询是否存在该用户
            UserDAOImpl userDAO=new UserDAOImpl();
            user=userDAO.FindUserByUsernameAndPassword(username,password);//userDAO查找用户，返回一个user类

            if(user==null){
                loginMsg="用户名或密码错误!";
                req.setAttribute("loginMsg",loginMsg);
                req.getRequestDispatcher("login").forward(req,resp);
                //如果user为空，说明找不到，用户名或密码错误
            }else{

                //记录登录时间，存到数据库中
                Log.getInstance().clear();//先清除数据,防止重复记录
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = simpleDateFormat.format(System.currentTimeMillis());
                session.setAttribute("lastTime",time);
                Log.getInstance().setLastTime(time);
                Log.getInstance().setUsername(username);
                LogDaoImpl logDao = new LogDaoImpl();
                logDao.InsertLastTime(Log.getInstance());
                uid = user.getUser_id();
                session.setAttribute("isLogged",true);
                session.setAttribute("uid",user.getUser_id());
                session.setAttribute("username",username);
                session.setAttribute("password",password);
                //    System.out.println("++ huai huai");
                userDAO.UpdataUserKey(uid);
                resp.sendRedirect("welcome");
                //否则的话用户存在，保存用户名和密码到session中，请求重定向到welcome
            }
        }
    }

    //判断用户名密码是否合法
    private boolean isValid(){
        if(this.username == null || this.username=="" || this.password==null || this.password==""){
            loginMsg="用户名或密码不能为空";
            return false;
        }
        if(this.loginCode==null || this.loginCode==""){
            loginMsg="验证码不能为空";
            return false;
        }
        if(!this.loginCode.equalsIgnoreCase(this.code)){
            loginMsg="验证码不正确";
            return false;
        }
        return true;
    }
}
